package edu.pw.p2pclient.networking.webservice;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class FoundResource {

	private String mName;
	private String mNetworkName;
	private String mNetworkPassword;
	private double mLatitude;
	private double mLongitude;
	private int mNetwork;
	private int mSize;
	private String mCrc;

	public FoundResource(String name, String networkName, String password,
			double latitude, double longitude, int network, int size, String crc) {
		mName = name;
		mNetworkName = networkName;
		mNetworkPassword = password;
		mLatitude = latitude;
		mLongitude = longitude;
		mNetwork = network;
		mSize = size;
		mCrc = crc;
	}

	public String getResName() {
		return mName;
	}

	public String getNetworkName() {
		return mNetworkName;
	}

	public String getNetworkPassword() {
		return mNetworkPassword;
	}

	public double getLatitude() {
		return mLatitude;
	}

	public double getLongitude() {
		return mLongitude;
	}

	public int getNetwork() {
		return mNetwork;
	}

	public int getSize() {
		return mSize;
	}

	public String getCrc() {
		return mCrc;
	}

	public final static JsonDeserializer<FoundResource> deserializer = new JsonDeserializer<FoundResource>() {

		@Override
		public FoundResource deserialize(JsonElement json, Type typeOfT,
				JsonDeserializationContext context) throws JsonParseException {

			String name = json.getAsJsonObject().get("name").getAsString();
			String networkName = json.getAsJsonObject().get("networkName")
					.getAsString();
			String password = json.getAsJsonObject().get("password")
					.getAsString();
			double latitude = json.getAsJsonObject().get("latitude")
					.getAsDouble();
			double longitude = json.getAsJsonObject().get("longitude")
					.getAsDouble();
			int network = json.getAsJsonObject().get("bluetooth").getAsInt();
			if (network == 0)
				network = 2;
			int size = json.getAsJsonObject().get("size").getAsInt();
			String crc = json.getAsJsonObject().get("crc").getAsString();

			return new FoundResource(name, networkName, password, latitude,
					longitude, network, size, crc);
		}
	};
}
